<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of accountDAO
 *
 * @author laimis
 */
class AccountDAO extends  BaseDAO{

    public function getAll(){
        $list ;
        $sql='SELECT * FROM `account` LIMIT 0,25 ;';
        $result = mysql_query($sql);
        while($row = mysql_fetch_object($result,'Account')) {
            $list[] = $row;
        }
        return $list;
    }

    public function getById($id){
        $sql='SELECT * FROM `account` WHERE `account`.`id`= '.$id.';';
        $result = mysql_query($sql);
        return mysql_fetch_object($result,'account');
    }

    public function getByName($username){
        $sql='SELECT * FROM `account` WHERE `account`.`user_name`= \''.$username.'\';';
        $result = mysql_query($sql);
        return mysql_fetch_object($result,'account');
    }

    public function insert($account){
        $id=  false;
        $sql='INSERT INTO `account` (
        `profile_id`,
        `username`,
        `password`,
        `email`) VALUES(
          '.$account->profile_id.' ,
        \''.$account->username.'\' ,
        \''.$account->password.'\' ,
        \''.$account->email.'\');';
        $result = mysql_query($sql);
        if($result){
            $id = mysql_insert_id();
        }
        return $id;
    }

    public function update($account){
        $sql='UPDATE `account` SET
        `profile_id`='.$account->profile_id.' ,
        `username`=\''.$account->username.'\' ,
        `password`=\''.$account->password.'\' ,
        `email` =\''.$account ->email.'\' WHERE `id`= '.$account->id.' ;';
        $result = mysql_query($sql);
    }

    public function delete($id){
        $sql='DELETE FROM `account` WHERE `id`='.$id.';';
        $result = mysql_query($sql);
    }


        /*
     * @method suskaiciojamas yrasu skaicius
     * @return yrasu skaicius
     */
    public function isAdmin($id){
        $sql ='SELECT * FROM `admin` WHERE `account_id`='.$id.';';
        $result = mysql_query($sql);
        if($result == false){
            return false;
        }else{
            if($result == $id){
                return true;
            }
        }
        return false;
    }

    public function AccountDAO(){
        $this->initDB();
    }
}
?>
